soda()
	._page('index')
	._api('')
	._module(['element', 'sysMain', 'sdLocal', 'sysTheme'])
	._run((unit) => {
		// Token 验证
		unit.sdHttp['get']({
			url: '/v1/platform/iam/token',
			loading: false,
			callback(d) {
			    mian.iam();
			    if((typeof d) == 'undefined' || d.length == 0){
			        alert('当前尚未配置任何可用权限,请联系管理员!')
					d = [];
			    }
			    mian.menu(d)
			    unit.sdDialog['removeLoading']();
			    $('#view-loading').hide();
			}
		});
		
		let mian = {
		    iam(){
		        let iam = unit.sdLocal['user']['get']();
		        if(iam){
		            $('#huName').text(iam.realName);
		            if((iam.avatar).length > 0){
		                $('#app-user-avatar').attr('src', iam.avatar);
		            }
		        }else{
		            unit.sdLocal['clean']();
		            location.replace('/login.html');
		        }
		    },
		    menu(menus) {
		        let app = unit.sdLocal['app']['get']();
		        $('#app-name').html("&nbsp;" + app.name);
		        let permissions = [];
		        $.each(menus, function(sysMain, item){
		            let sub = item.sub;
		            $.each(sub, function(i, t){
		                permissions.push(t);
		            })
		        });
		        unit.sdLocal['menus']['put'](permissions)
		        $('#admin-side-nav').vm({menus: menus});
		        unit.element['render']('nav');
		        // 把面板页加入路由表中
		        let panel = {url: app.panel, type: 1};
		        menus.push(panel)
		        unit.sysMain['regRouter'](menus);  // 注册路由
		        unit.sysMain['loadHome']({  // 加载主页
		            url: '#' + app.panel,
		            name: '<i class="layui-icon layui-icon-home"></i>'
		        });
		    }
		}
		
		unit.sysTheme['initTheme']();
	})
